import request from '@neu/biz-core/utils/request'
import axios from 'axios'
import { getToken } from '@neu/biz-core/utils/auth'

export const pageBySchema = (schemadefId) => {
  const url = '/cmdb/instances/schema/' + schemadefId + '/page'
  return (params) => {
    return request({
      url: url,
      params,
      method: 'get'
    })
  }
}

export const exportBySchema = (schemadefId, params) => {
  const url = '/cmdb/instances/schema/' + schemadefId + '/export'
  return new Promise((resolve, reject) => {
    axios.get(url, { params, responseType: 'blob', headers: { 'Authentication': getToken() }})
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export const exportGlobal = (params) => {
  const url = '/cmdb/instances/export'
  return new Promise((resolve, reject) => {
    axios.get(url, { params, responseType: 'blob', headers: { 'Authentication': getToken() }})
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export const downloadTpl = () => {
  const url = '/cmdb/instances/tpl'
  return new Promise((resolve, reject) => {
    axios.get(url, { responseType: 'blob', headers: { 'Authentication': getToken() }})
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export const listBySchema = (schemadefId) => {
  const url = '/cmdb/instances/schema/' + schemadefId
  return request({
    url: url,
    method: 'get'
  })
}

export const detail = (id) => {
  return request({
    url: '/cmdb/instances/' + id,
    method: 'get'
  })
}

export const update = (id, data) => {
  return request({
    url: '/cmdb/instances/' + id,
    data: data,
    method: 'put'
  })
}

export const create = (data) => {
  return request({
    url: '/cmdb/instances',
    data: data,
    method: 'post'
  })
}

export const remove = (id) => {
  return request({
    url: '/cmdb/instances/' + id,
    method: 'delete'
  })
}

export const batchRemove = (ids) => {
  return request({
    url: '/cmdb/instances/batch',
    data: { ids },
    method: 'delete'
  })
}

export const schemaInstances = (schemadefId) => {
  return request({
    url: '/cmdb/instances/schema/' + schemadefId,
    method: 'get'
  })
}

export const getRelations = (id) => {
  const url = '/cmdb/instances/' + id + '/relations'
  return () => {
    return request({
      url: url,
      method: 'get'
    })
  }
}

export const addRelation = (instanceId, data) => {
  return request({
    url: '/cmdb/instances/' + instanceId + '/relations/add',
    data,
    method: 'put'
  })
}

export const removeRelation = (instanceId) => {
  const urlPrefix = '/cmdb/instances/' + instanceId + '/relations/'
  return (relationId) => {
    const url = urlPrefix + relationId
    return request({
      url: url,
      method: 'delete'
    })
  }
}

export const updateAttr = (instanceId, attrKey, attrVal) => {
  return request({
    url: '/cmdb/instances/' + instanceId + '/' + attrKey,
    data: { value: attrVal },
    method: 'put'
  })
}

export const getTopo = (instanceId) => {
  return request({
    url: '/cmdb/instances/' + instanceId + '/topo',
    method: 'get'
  })
}

export const globalSearch = (params) => {
  return request({
    url: '/cmdb/instances/search',
    params,
    method: 'get'
  })
}

export const upload = (schemaId) => {
  return '/cmdb/instances/schema/' + schemaId + '/import'
}

export const batchUpdateOneAttr = (schemaId, attrKey, attrVal) => {
  return request({
    url: '/cmdb/instances/batch/' + schemaId + '/' + attrKey,
    data: { value: attrVal },
    method: 'put'
  })
}

export const countOfInstances = () => {
  return request({
    url: '/cmdb/instances/count',
    method: 'get'
  })
}

export const statsByCategory = () => {
  return request({
    url: '/cmdb/instances/stats/category',
    method: 'get'
  })
}

